//openssl lib  :   libeay32   and   ssleay32


#include <iostream>
#include <string>
extern "C" {
#include <openssl/bio.h>
#include <openssl/rand.h>
}

class Rand
{
public:
	std::string getRandomString(int num);
private:
	static const char * numbersAndLetters ;
};

const char * Rand::numbersAndLetters = "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

std::string Rand::getRandomString(int num)
{
	std::string rand;
	unsigned char out[num];
	int ret,len;
	char filename[50] = {0};

#ifdef _WIN32
	RAND_screen();
#endif

   	while(1) {
    	ret=RAND_status();
		if(ret==1) {
     		printf("seeded enough!\n");
     		break;
		}
    	else {
     		printf("not enough sedded!\n");
     		RAND_poll();
    	}
	}

	const char * p=::RAND_file_name(filename,50);

   	if(p==NULL) {
    	printf("can not get rand file\n");
    	return "";
   	}

   	ret=RAND_write_file(p);
   	len=RAND_load_file(p,50);
   	ret=RAND_bytes(out, num);

	for(int i = 0; i < num; i++) {
		rand.push_back(numbersAndLetters[(int)(out[i])%71]);
	}

	return rand;
}

int
main()
{
	Rand r;
	std::string s = r.getRandomString(10);
	std::cout << s << std::endl;
}
